package com.examination.system.administrato;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Aircondition{
    private Integer number= 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public   void increment () throws Exception{
        lock.lock();
        try {
            //判断
            while(number!=0){
                condition.await();
            }
            //干活
            number++;
            System.out.println(Thread.currentThread().getName()+":\t"+number);
            //通知
            condition.notifyAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
    public    void decrement () throws Exception{
        lock.lock();
        try {
            //判断
            while(number ==0){
                condition.await();
            }
            //干活
            number--;
            System.out.println(Thread.currentThread().getName()+":\t"+number);
            //通知
            condition.notifyAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }





  /*  public  synchronized void increment () throws Exception{
        //判断
        while(number!=0){
            this.wait();
        }
        //干活
        number++;
        System.out.println(Thread.currentThread().getName()+":\t"+number);
        //通知
        this.notifyAll();
    }
    public synchronized   void decrement () throws Exception{
       //判断
        while (number==0){
            this.wait();
        }
        //干活
        number--;
        System.out.println(Thread.currentThread().getName()+":\t"+number);
        //通知
        this.notifyAll();
    }*/

}
/**
 * @author 寇淇芮
 * @Date 2019/10/14 08:33
 * 题目 现在两个线程,可以操作初始值为零的一个变量,
 * 实现一个线程对该变量加1 一个变量减一
 * 实现交替 来10轮变量初始值为零
 *       `1.高聚低耦合前提下,线程操作资源类
 *        2.判断/干活/通知
 *        3.
 */
public class prodCosnsumerDome04 {
    public static void main(String[] args) {
        Aircondition aircondition = new Aircondition();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                    aircondition.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"寇淇芮").start();
        new Thread(()->{
        for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                    aircondition.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"王珊珊").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                    aircondition.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"田东升").start();
        new Thread(()->{
        for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                    aircondition.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"邓有方").start();
    }
}
